/*
 * @Author: szx
 * @Date: 2022-07-20 14:22:14
 * @LastEditTime: 2022-07-20 14:47:25
 * @Description:
 * @FilePath: \leetcode\500-599\558\558.js
 */
function Node(val, isLeaf, topLeft, topRight, bottomLeft, bottomRight) {
    this.val = val;
    this.isLeaf = isLeaf;
    this.topLeft = topLeft;
    this.topRight = topRight;
    this.bottomLeft = bottomLeft;
    this.bottomRight = bottomRight;
}

/**
 * @param {Node} quadTree1
 * @param {Node} quadTree2
 * @return {Node}
 */
var intersect = function (quadTree1, quadTree2) {
    if (quadTree1.isLeaf) {
        if (quadTree1.val) return new Node(true, true);
        return new Node(quadTree2.val, quadTree2.isLeaf, quadTree2.topLeft, quadTree2.topRight, quadTree2.bottomLeft, quadTree2.bottomRight);
    }
    if (quadTree2.isLeaf) return intersect(quadTree2, quadTree1);
    const topLeft = intersect(quadTree1.topLeft, quadTree2.topLeft);
    const topRight = intersect(quadTree1.topRight, quadTree2.topRight);
    const bottomLeft = intersect(quadTree1.bottomLeft, quadTree2.bottomLeft);
    const bottomRight = intersect(quadTree1.bottomRight, quadTree2.bottomRight);
    if (topLeft.isLeaf && topRight.isLeaf && bottomLeft.isLeaf && bottomRight.isLeaf && topLeft.val === topRight.val && topLeft.val === bottomLeft.val && topLeft.val === bottomRight.val)
        return new Node(topLeft.val, true);
    return new Node(false, false, topLeft, topRight, bottomLeft, bottomRight);
};
